# -*- coding: utf-8 -*-
__author__ = 'BuddyHolly'

import os
from PIL import Image, ImageDraw, ImageFont, ImageOps


# =====================================
# *** 盲水印生成 ***
#
# text：水印文字
# filename：文件名（无后缀，多传入hash内容）
# save_path：存储路径
# inv：水印图是否反相
#
# 注意：只接收处理十个字水印内容！
# 返回生成水印图的路径地址tem_path
# =====================================

def gen_wm(text, filename, save_path, inv=False):
    # 创建水印图
    img = Image.new('L', (270, 135), 255)
    draw = ImageDraw.Draw(img)
    text_to_draw = text
    font = ImageFont.truetype(os.path.join(os.getcwd(), r'app\static\fonts', 'NotoSansCJK-Black.ttc'), 50)
    # 在水印图上添加字样
    draw.text((10, 0), text_to_draw[:5], font=font)
    if len(text_to_draw) > 5:
        draw.text((10, 50), text_to_draw[5:10], font=font)
    del draw

    # 保存水印图
    filename = filename + '.png'
    tem_path = os.path.join(save_path, filename)

    # 图片反相
    if inv:
        inverted_image = ImageOps.invert(img)
        inverted_image.save(tem_path)
    else:
        img.save(tem_path)

    return tem_path
